package com.ntt.register.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ntt.register.biz.AdminBiz;
import com.ntt.register.biz.impl.AdminBizImpl;
import com.ntt.vo.Admin;

/**
 * Servlet implementation class AdimnController
 */
public class AdminController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminBiz adminBiz = new AdminBizImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String action = request.getParameter("action");
		
		if("login".equals(action)){
			String admin_name = request.getParameter("user");
			String admin_password = request.getParameter("password");
			
			Admin admin = adminBiz.adminLogin(admin_name, admin_password);
			
			if(admin == null){
				request.setAttribute("msg", "用户名或密码错误！");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{
				request.getSession().setAttribute("admin", admin);
				response.sendRedirect("register.jsp");
			}
		}
	}
}
